package com.zhanggm.springcloud.jpa.dao;

import com.zhanggm.springcloud.jpa.entity.UserEntity;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.data.repository.query.Param;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

public interface UserRepository extends JpaRepository<UserEntity,Integer>, JpaSpecificationExecutor<UserEntity> {
    //根据用户名称模糊查询
    List<UserEntity> findByUsernameLike(String username);
    //分页排序
//    Pageable findByUsernameLike(String username, Pageable pageable);

    UserEntity findByUsernameAndPassword(String username,String password);

    Page<UserEntity> findByUsernameAndPassword(String username, String password, Pageable pageable);
    //自定义删除
    @Transactional
    int deleteByUsernameAndPassword(String username,String password);

    //原生Sql
    @Query(value = "select * from t_user_entity where username=:unmae",nativeQuery = true)
    List<UserEntity> selectByUsername(@Param("unmae") String username);

    @Query(value = "select * from t_user_entity where username=:uname",nativeQuery = true)
    Page<UserEntity> selectByUsername(@Param("uname") String username,Pageable pageable);

    //更新和删除都是需要@Modifying、@Transactional
    @Transactional
    @Modifying
    @Query(value = "update t_user_entity set password=:pwd where username=:uname",nativeQuery = true)
    int updatePasswordByUsername(@Param("uname") String usernmae,@Param("pwd") String password);

}
